package com.csii.adhoc.dataunits.dao;

import com.csii.adhoc.board.filter.FilterUnitPo;
import com.csii.adhoc.board.mapper.FilterUnitMapper;
import com.csii.adhoc.dataready.TDataCondPo;
import com.csii.adhoc.dataready.TDataNewfun;
import com.csii.adhoc.dataready.mapper.*;
import com.csii.adhoc.dataunits.addcolumn.ColValueOrder;
import com.csii.adhoc.dataunits.addcolumn.DataNewFld;
import com.csii.adhoc.dataunits.mapper.UnitAdnMapper;
import com.csii.adhoc.dataunits.*;
import com.csii.adhoc.dataunits.UnitBase.UnitBasePo;
import com.csii.adhoc.dataunits.UnitBase.UnitBaseVo;
import com.csii.adhoc.dataunits.mapper.*;
import com.csii.adhoc.dataunits.service.DataUnitUtils;
import com.csii.common.DbLinkUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedCaseInsensitiveMap;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Repository
public class DataUnitsDao {

    private final static  Object lock = new Object();
    private final static  Object lock2 = new Object();

    @Resource
    DataUnitsMapper dataUnitsMapper;
    @Resource
    UnitKeyMapper unitKeyMapper;
    @Resource
    UnitCvgMapper unitCvgMapper;
    @Resource
    UnitGroupMapper unitGroupMapper;
    @Resource
    TDataCondMapper dataCondMapper;
    @Autowired
    DbLinkUtils dbLinkUtils;
    @Resource
    private UnitBaseMapper unitBaseMapper;
    @Resource
    private TDataTaskMapper tDataTaskMapper;
    @Resource
    private UnitAdnMapper unitAdnMapper;
    @Resource
    private TDataNewFunMapper tDataNewFunMapper;
    @Resource
    private TDataNewFldMapper tDataNewFldMapper;
    @Resource
    private FilterUnitMapper filterUnitMapper;
    @Resource
    private TDataOrdMapper tDataOrdMapper;
    @Resource
    private TTextGrpMapper tTextGrpMapper;
    @Resource
    private TDataGrpMapper tDataGrpMapper;


    public List<Map<String, Object>> generalQueryList(String connId, String sql) {
        // 从 spring 容器中获取 jdbcTemplate
        JdbcTemplate jdbcTemplate = dbLinkUtils.getJdbcTemplate(connId);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    public Map<String, List<Object>> generalQueryMapDao(String connId, String sql) {

        // 从 spring 容器中获取 jdbcTemplate
        JdbcTemplate jdbcTemplate = dbLinkUtils.getJdbcTemplate(connId);
        //从数据库查询结果集
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        Map<String, List<Object>> mapCaseInsensitive = new LinkedCaseInsensitiveMap<>();
        for (Map<String, Object> map : list) {
            if (list.get(0) == null) {
                break;
            }
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String colName = entry.getKey();
                Object valObject = entry.getValue() == null ? "" : entry.getValue();
                List<Object> listObject = mapCaseInsensitive.get(colName);
                if (listObject == null) {
                    listObject = new ArrayList<>();
                }
                listObject.add(valObject);
                mapCaseInsensitive.put(colName, listObject);
            }
        }
        return mapCaseInsensitive;
    }

    public List<Map<String, Object>> generalDataQuery(String sql) {
        /*
         因 mapper 中使用了 ${}，且只能使用 ${}，不能使用 #{},这会造成 sql 注入风险
         在这里，做 sql 防注入处理
         */
        sql = sql.replaceAll("([;])+|(--)+", "");
        List<Map<String, Object>> list = dataUnitsMapper.generalSqlQuery(sql);
        return list;
    }

    public List<Map<String, Object>> choiceTable(String sql) {
        List<Map<String, Object>> list = dataUnitsMapper.generalSqlQuery(sql);
        return list;
    }

    public List<Map<String, Object>> getColumnGroupBy(String sql, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> list = dataUnitsMapper.generalSqlQuery(sql);
        return list;
    }

    @Transactional
    public int insertOneUnitBase(UnitBasePo record) {
        return unitBaseMapper.insertOneUnitBase(record);
    }

    @Transactional
    public int insertOneUnitKey(UnitKey key) {
        synchronized (lock) {
            return unitKeyMapper.insert(key);
        }
    }

    public int insertOneUnitCvg(UnitCvg unitCvg) {
        synchronized ( lock2) {
            return unitCvgMapper.insert(unitCvg);
        }
    }

    public int insertOneUnitGroup(UnitGroup unitGroup) {
        return unitGroupMapper.insert(unitGroup);
    }

    public List<UnitBaseVo> queryUnitBaseById(String boardId) {
        return unitBaseMapper.selectByDashboardId(boardId);
    }

    public void deleteUnitKeyByPrimaryKey(UnitKeyKey key) {
        synchronized (lock) {
            unitKeyMapper.deleteByPrimaryKey(key);
        }
    }

    public List<UnitKey> getAllUnitKey(String unitId, String type) {
        synchronized (lock) {
            return unitKeyMapper.getAllUnitKey(unitId, type);
        }
    }

    public void deleteAllUnitCvgByUnitId(String unitId) {
        synchronized ( lock2) {
            unitCvgMapper.deleteByUnitId(unitId);
        }

    }

    public void deleteAllCondition(String unitId) {
        dataCondMapper.deleteCondByUnitId(unitId);
    }

    public void deleteUnitKeyByUnitId(String unitId, String type) {
        synchronized ( lock) {
            unitKeyMapper.delAllByUnitId(unitId, type);
        }
    }

    public Integer getUnitType(String unitId) {
        return unitBaseMapper.getUnitType(unitId);
    }

    public List<UnitCvg> getAllUnitCvg(String unitId) {
        synchronized ( lock2) {
            return unitCvgMapper.getAllUnitCvg(unitId);
        }
    }

    public Map<String, String> getAllCondition(String unitId) {
        return dataCondMapper.getAllCondByUnitId(unitId);
    }

    public void deleteAllWidgets(String unitId, String type) {
        unitAdnMapper.deleteAllWidgets(unitId, type);
    }

    public void insertAdnInfo(String unitId, String widgets, String type) {
        TUnitAdn tUnitAdn = new TUnitAdn();
        tUnitAdn.setUnitId(unitId);
        tUnitAdn.setAddInfo(widgets);
        tUnitAdn.settType(type);
        unitAdnMapper.insertSelective(tUnitAdn);
    }

    public String getAllWidgets(String unitId, String type) {
        return unitAdnMapper.getWidgetByUnitId(unitId, type);
    }

    public String getTableIdByModelId(String modelId) {
        return tDataTaskMapper.getTableIdByModelId(modelId);
    }

    public void updateUnitBase(UnitBasePo unitBasePo) {
        unitBaseMapper.updateByPrimaryKey(unitBasePo);
    }

    public String selectModelByUnitId(String unitId) {
        return unitBaseMapper.selectModelIdByUnitId(unitId);
    }

    public UnitBasePo selectUnitInfoById(String unitId) {
        return unitBaseMapper.selectByPrimaryKey(unitId);
    }

    public List<TDataNewfun> getAllFunc(String type) {
        List<TDataNewfun> tDataNewfuns = tDataNewFunMapper.selectAllFunMapBean();
        return tDataNewfuns;
    }

    public void insertNewFld(DataNewFld dnf) {
        tDataNewFldMapper.insert(dnf);
    }

    public List<FilterUnitPo> getFilterUnitCond(String unitId) {
        List<FilterUnitPo> filterUnitPos = filterUnitMapper.selectListFilterUnitByUnitId(unitId);
        return filterUnitPos;
    }

    public Map<String, Object> getIntColumnInfo(String sql) {
        List<Map<String, Object>> list = dataUnitsMapper.generalSqlQuery(sql);
        Map map = list.get(0);
        return map;
    }

    public List<Map<String, Object>> getIntColumnInfo2(String sql) {
        return dataUnitsMapper.generalSqlQuery(sql);
    }

    public void updateNewFld(DataNewFld dnf) {
        tDataNewFldMapper.updateByPrimaryKey(dnf);
    }

    public void insertOrderTable(TDataOrd order) {
        tDataOrdMapper.insert(order);
    }

    public void updateOrderTable(TDataOrd dataOrd) {
        tDataOrdMapper.update(dataOrd);
    }

    public void insertTextGrp(TTextGrp textGrp) {
        tTextGrpMapper.insert(textGrp);
    }

    public void insertNumberGrp(TDataGrp tDataGrp) {
        tDataGrpMapper.insert(tDataGrp);
    }

    public void insertFun() {
        TDataNewfun tDataNewfun = new TDataNewfun();
        tDataNewfun.setFunId(DataUnitUtils.uuid());
        tDataNewfun.setFunClass("1");
        tDataNewfun.setFunName("ssss");
        tDataNewfun.setRetType(new BigDecimal("1"));
        tDataNewfun.setParaCount(new BigDecimal("2"));
        tDataNewfun.setFunMark("kkdfkdkf");
        tDataNewfun.setCreatedBy("admin");
        tDataNewfun.setCreatedDt("admin");
        tDataNewfun.setUpdatedBy("admin");
        tDataNewfun.setUpdatedDt("admin");
        tDataNewFunMapper.insert(tDataNewfun);
        TDataNewfun tDataNewfun1 = tDataNewFunMapper.selectByPrimaryKey(tDataNewfun.getFunId());
        tDataNewfun1.setFunMark("专用修改测试");
        tDataNewFunMapper.updateByPrimaryKey(tDataNewfun1);
    }

    public List<TDataNewfun> getTDataFun() {
        return tDataNewFunMapper.getTDataFun();
    }

    public List<DataNewFld> getNewFieldsByModelId(String modelId) {
        return tDataNewFldMapper.selectAllFieldByModelId(modelId);
    }

    public DataNewFld getNewFieldsById(String colId) {
        return tDataNewFldMapper.getNewFieldsById(colId);
    }

    public void updateUnitBaseModelId(String unitId, String modelId) {
        UnitBasePo unitBasePo = new UnitBasePo();
        unitBasePo.setUnitId(unitId);
        unitBasePo.setModelId(modelId);
        unitBaseMapper.updateModelIdByUnitId(unitBasePo);
    }

    public List<TDataOrd> getOrderByType(String modelId,String colName, String type) {
        TDataOrd tDataOrd = new TDataOrd();
        tDataOrd.setModelId(modelId);
        tDataOrd.setColName(colName);
        tDataOrd.setType(type);
        return tDataOrdMapper.getOrderByType(tDataOrd);
    }

    public List<TTextGrp> getTextGrpByModelId(TTextGrp record) {
        return tTextGrpMapper.getTextGrpByModelId(record);
    }

    public List<TDataGrp> getDataGrpByModelId(NumberGroup dataGrp) {
        return tDataGrpMapper.getDataGrpByModelId(dataGrp);
    }

    public DataNewFld selectAddedColumnById(String fieldId) {
        return tDataNewFldMapper.selectByPrimaryKey(fieldId);
    }

    public void deleteOrderByType(String modelId, String type) {
        tDataOrdMapper.deleteOrderByType(modelId,type);
    }

    public void deleteAddedColumnById(String fieldId) {
        tDataNewFldMapper.deleteByPrimaryKey(fieldId);
    }

    public void deleteTextGrp(String fieldId) {
        tTextGrpMapper.deleteTextGrpByFieldId(fieldId);
    }
    public void deleteNumberGrp(String fieldId) {
        tDataGrpMapper.deleteNumberGrpByFld(fieldId);
    }

    public void deleteOrder(String modelId, String colName, String type) {
        tDataOrdMapper.deleteOrder(modelId,colName,type);
    }

    public List<DataNewFld> queryAllNewFldByModelId(String modelId) {
        return tDataNewFldMapper.selectListDataNewFld(modelId);
    }

    public void deleteTextGrpByColumn(String modelId,String fieldId) {
        tTextGrpMapper.deleteTextGrpByColumn(modelId, fieldId);
    }

    public void deleteNumberGrpByFld(String fieldId) {
        tDataGrpMapper.deleteNumberGrpByFld(fieldId);
    }
}
